/*
 * @Date: 2025-05-11 14:30:39
 * @LastEditors: 不吃芒果 syuyi_1994@163.com
 * @LastEditTime: 2025-05-22 22:02:00
 */
/**
 * API使用示例
 */
import http from './http.js';

// 使用基础的HTTP方法
const customGetRequest = async (url, params) => {
  try {
    return await http.get(url, params);
  } catch (error) {
    console.error('请求失败:', error);
    throw error;
  }
};

const customPostRequest = async (url, data) => {
  try {
    return await http.post(url, data);
  } catch (error) {
    console.error('请求失败:', error);
    throw error;
  }
};

// 使用预配置的API方法（自动关联apiUrls）
const login = async (code) => {
  try {
    const result = await http.api.login({
      type: 34,
      code: code,
      state: 'shequan',
      method: 'POST'
    });
    console.log(result);
    // 处理登录成功
    uni.setStorageSync('token', result.accessToken);
    uni.setStorageSync('us_oid', result.openid);
    return result;
  } catch (error) {
    // 处理登录失败
    console.error('登录失败:', error);
    throw error;
  }
};

// 获取用户信息示例
const getUserInfo = async () => {
  try {
    return await http.api.userInfo({
      method: 'GET'
    });
  } catch (error) {
    console.error('获取用户信息失败:', error);
    throw error;
  }
};

// 更新用户信息示例
const updateUserInfo = async (data) => {
  try {
    return await http.api.updateUserInfo({
      method: 'PUT',
      ...data
    });
  } catch (error) { 
    console.error('更新用户信息失败:', error);
    throw error;
  }
};


// 文件上传示例
const uploadFile = async (filePath) => {
  try {
    return await http.upload('/infra/file/upload', filePath, 'file', {
      method: 'UPLOAD'
    });
  } catch (error) {
    console.error('文件上传失败:', error);
    throw error;
  }
};

export {
  login,
  getUserInfo,
  updateUserInfo,
  customGetRequest,
  customPostRequest,
  uploadFile
}; 